package smedi.ismedi.web.bff.controller.quality;
import cn.hutool.core.collection.CollectionUtil;
import com.smedi.ismedi.inmail.api.dto.request.ProcessTaskListRequest;
import com.smedi.ismedi.inmail.api.dto.response.CommentResponse;
import com.smedi.ismedi.inmail.api.dto.response.ProcessTaskResponse;
import com.smedi.ismedi.inmail.api.feign.RemoteProcessFeignApi;
import com.smedi.ismedi.instrument.api.dto.request.hiddenDanger.*;
import com.smedi.ismedi.instrument.api.dto.response.PersonResponse;
import com.smedi.ismedi.instrument.api.dto.response.hiddenDanger.*;
import com.smedi.ismedi.instrument.api.feign.RemoteQualityHiddenDangerFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smedi.ismedi.web.bff.dto.quality.QualityHiddenDangerDetailResponse;
import smedi.ismedi.web.bff.dto.quality.RectificationRecordResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(value = "质量隐患接口", tags = {"质量隐患接口"})
@RequestMapping(value = "/webbff/v1/qualityHiddenDanger")
@RestController
@AllArgsConstructor
public class QualityHiddenDangerController {
    private final RemoteQualityHiddenDangerFeignApi remoteQualityHiddenDangerFeignApi;

    private final RemoteProcessFeignApi processFeignApi;


    /**
     * 质量隐患保存
     */
    @ApiOperation(value = "质量隐患保存", nickname = "质量隐患保存")
    @PostMapping(value = "/saveQualityHiddenDanger")
    ResponseData<QualityHiddenDangerResponse> saveQualityHiddenDanger(@Validated @RequestBody QualityHiddenDangerRequest saveQualityHiddenDangerRequest){
        return remoteQualityHiddenDangerFeignApi.saveQualityHiddenDanger(saveQualityHiddenDangerRequest);
    }

    /**
     * 质量隐患删除
     */
    @ApiOperation(value = "质量隐患删除", nickname = "质量隐患删除")
    @PostMapping(value = "/delQualityHiddenDanger")
    ResponseData<Boolean> delQualityHiddenDanger(@Validated @RequestBody DelQualityHiddenDangerRequest delQualityHiddenDangerRequset){
        return remoteQualityHiddenDangerFeignApi.delQualityHiddenDanger(delQualityHiddenDangerRequset);
    }


    /**
     * 质量隐患分页查询
     */
    @ApiOperation(value = "质量隐患分页查询", nickname = "质量隐患分页查询")
    @PostMapping(value = "/queryQualityHiddenDangerPage")
    ResponseData<PaginationData<QualityHiddenDangerResponse>> queryQualityHiddenDangerPage(@Validated @RequestBody QueryQualityHiddenDangerPageRequest queryQualityHiddenDangerPageRequest){
        return remoteQualityHiddenDangerFeignApi.queryQualityHiddenDangerPage(queryQualityHiddenDangerPageRequest);
    }


    /**
     * 质量隐患单个查询
     */
    @ApiOperation(value = "质量隐患单个查询", nickname = "质量隐患单个查询")
    @PostMapping(value = "/queryQualityHiddenDangerByOne")
    ResponseData<QualityHiddenDangerDetailResponse> queryQualityHiddenDangerByOne(@Validated @RequestBody QueryQualityHiddenDangerByOneRequest queryQualityHiddenDangerByOneRequest){
        QualityHiddenDangerDetailResponse qualityHiddenDangerDetailResponse = new QualityHiddenDangerDetailResponse();
        ResponseData<QualityHiddenDangerResponse> responseResponseData = remoteQualityHiddenDangerFeignApi.queryQualityHiddenDangerByOne(queryQualityHiddenDangerByOneRequest);
        if(responseResponseData.isSuccess()){
            QualityHiddenDangerResponse qualityHiddenDangerResponse = responseResponseData.getData();
            qualityHiddenDangerDetailResponse.setQualityHiddenDangerResponse(qualityHiddenDangerResponse);
            com.smedi.ismedi.web.starter.common.ResponseData<List<ProcessTaskResponse>> listResponseData = processFeignApi.getProcessTaskList(ProcessTaskListRequest.builder()
                    .businessCode(String.valueOf(queryQualityHiddenDangerByOneRequest.getId()))
                    .businessType("QLT_HIDDEN_DANGER")
                    .processDefinitionKey("PROCESS_QLT_HIDDENDANGER_RECTIFICATION")
                    .build());

            List<RectificationRecordResponse> rectificationRecords = new ArrayList<>();
            if(listResponseData.isSuccess() && CollectionUtil.isNotEmpty(listResponseData.getData())){
                List<ProcessTaskResponse> processTaskResponses = listResponseData.getData();
                for(ProcessTaskResponse processTaskResponse :processTaskResponses){
                    String job = "";
                    String result = "";
                    Map<String, String> variables = processTaskResponse.getVariables();
                    if(CollectionUtil.isNotEmpty(variables)){
                        for (String name : variables.keySet()) {
                            if ("PERSONJOB".equals(name)) {
                                job = variables.get(name);
                            }
                            if ("METHOD".equals(name)) {
                                result = variables.get(name);
                            }
                        }
                    }
                    if(null!=processTaskResponse.getEndTime()) {
                        //最后操做人
                        if(null != processTaskResponse.getOwner()){
                            Long ownerId = processTaskResponse.getOwner().getId();
                            String ownername = processTaskResponse.getOwner().getName();
                            qualityHiddenDangerResponse.setLastOperaPerson(new PersonResponse(ownerId,ownername,job));
                            qualityHiddenDangerResponse.setLastOperaTime(processTaskResponse.getEndTime());
                        }
                        if(("ACTIVITY_HIDDENDANGER_RECTIFICATION".equals(processTaskResponse.getTaskDefinitionKey())
                                || "ACTIVITY_HIDDENDANGER_REVIEW".equals(processTaskResponse.getTaskDefinitionKey()))
                                &&  null != processTaskResponse.getOwner()){
                            RectificationRecordResponse rectificationRecord = new RectificationRecordResponse();
                            Long ownerId = processTaskResponse.getOwner().getId();
                            String ownername = processTaskResponse.getOwner().getName();

                            rectificationRecord.setId(processTaskResponse.getId());
                            rectificationRecord.setTaskDefinitionKey(processTaskResponse.getTaskDefinitionKey());
                            rectificationRecord.setName(processTaskResponse.getName());
                            rectificationRecord.setStartTime(processTaskResponse.getStartTime());
                            rectificationRecord.setEndTime(processTaskResponse.getEndTime());
                            rectificationRecord.setReviewResult(result);
                            rectificationRecord.setPersonResponse(new PersonResponse(ownerId, ownername, job));
                            rectificationRecord.setComments(processTaskResponse.getComments());
                            rectificationRecord.setFileResponse(processTaskResponse.getFiles());
                            rectificationRecords.add(rectificationRecord);


                        }
                        if("deleted".equals(processTaskResponse.getDeleteReason())){
                            List<CommentResponse> commentResponseList = processTaskResponse.getComments();
                            qualityHiddenDangerDetailResponse.getQualityHiddenDangerResponse().setCloseTime(processTaskResponse.getEndTime());
                            if(CollectionUtil.isNotEmpty(commentResponseList)){
                                qualityHiddenDangerDetailResponse.getQualityHiddenDangerResponse().setCloseReason(commentResponseList.get(0).getFullMessage());
                            }
                        }

                    }
                }
            }
            qualityHiddenDangerDetailResponse.setRectificationRecords(rectificationRecords);

        }

        return new ResponseData<>().success(qualityHiddenDangerDetailResponse);
    }


    /**
     * 关闭质量隐患
     */
    @ApiOperation(value = "关闭质量隐患", nickname = "关闭质量隐患")
    @PostMapping(value = "/closeQualityHiddenDanger")
    ResponseData<CloseQualityHiddenDangerResponse> closeQualityHiddenDanger(@Validated @RequestBody CloseQualityHiddenDangerRequest closeQualityHiddenDangerRequest){
        return remoteQualityHiddenDangerFeignApi.closeQualityHiddenDanger(closeQualityHiddenDangerRequest);
    }


    /**
     * 整改
     */
    @ApiOperation(value = "质量隐患整改", nickname = "质量隐患-整改")
    @PostMapping(value = "/rectificationQualityHiddenDanger")
    ResponseData<RectificationResponse> rectificationQualityHiddenDanger(@Validated @RequestBody RectificationRequest rectificationRequest){
        return remoteQualityHiddenDangerFeignApi.rectificationQualityHiddenDanger(rectificationRequest);
    }

    /**
     * 复查
     */
    @ApiOperation(value = "质量隐患复查", nickname = "质量隐患-复查")
    @PostMapping(value = "/reviewQualityHiddenDanger")
    ResponseData<ReviewResponse> reviewQualityHiddenDanger(@Validated @RequestBody ReviewRequest reviewRequest){
        return remoteQualityHiddenDangerFeignApi.reviewQualityHiddenDanger(reviewRequest);
    }

    /**
     * 导出
     */
    @ApiOperation(value = "质量隐患导出", nickname = "质量隐患导出")
    @PostMapping(value = "/exportQualityHiddenDanger")
    ResponseEntity<byte[]> exportQualityHiddenDanger(@Validated @RequestBody ExportQualityHiddenDangerRequest exportQualityHiddenDangerRequest){
        return remoteQualityHiddenDangerFeignApi.exportQualityHiddenDanger(exportQualityHiddenDangerRequest);
    }

    /**
     *
     */
    @ApiOperation(value = "质量隐患移交", nickname = "质量隐患移交")
    @PostMapping(value = "/changeQualityHiddenDangerRectification")
    ResponseData<ChangeRectificationResponse> changeQualityHiddenDangerRectification(@Validated @RequestBody ChangeRectificationRequest changeRectificationRequest){
        return remoteQualityHiddenDangerFeignApi.changeQualityHiddenDangerRectification(changeRectificationRequest);
    }

    /**
     * 质量隐患部分字段编辑
     */
    @ApiOperation(value = "质量隐患部分字段编辑", nickname = "质量隐患部分字段编辑")
    @PostMapping(value = "/editQualityHiddenDanger")
    ResponseData<QualityHiddenDangerResponse> editQualityHiddenDanger(@Validated @RequestBody EditQualityHiddenDangerRequest editQualityHiddenDangerRequest){
        return remoteQualityHiddenDangerFeignApi.editQualityHiddenDanger(editQualityHiddenDangerRequest);
    }


}
